//
//  TalkAllQuestionsView.m
//  talkDemo
//
//  Created by telen on 15/4/20.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TalkAllQuestionsView.h"
#import "TalkDefine.h"


@implementation TalkAllQuestionsView
{
    UIButton* btnRecord;
    UIButton* btnPlay;
    UIButton* btnShare;
    UIButton* btnDelete;
    //btnPoint
    CGPoint bpoint_a;
    CGPoint bpoint_b;//center
    CGPoint bpoint_c;
    CGPoint bpoint_d;
}

@synthesize recordBtn = btnRecord;
@synthesize playBtn = btnPlay;
@synthesize shareBtn = btnShare;
@synthesize deleteBtn = btnDelete;

+ (TalkAllQuestionsView *)talkAllQuestions:(NSArray *)qarr ratio:(CGFloat)ratio_w
{
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    CGRect frame = CGRectMake(0, height, 768/ratio_w, (1024-506)/ratio_w);
    return [[TalkAllQuestionsView alloc] initWithFrame:frame questions:(NSArray *)qarr ratio:ratio_w];
}

- (instancetype)initWithFrame:(CGRect)frame_self questions:(NSArray *)qarr ratio:(CGFloat)ratio_w
{
    if (self = [super initWithFrame:frame_self]) {
        self.ratio = ratio_w;
        //
        bpoint_a = CGPointMake(188/ratio_w, 48/ratio_w);
        bpoint_b = CGPointMake(384/ratio_w, 48/ratio_w);
        bpoint_c = CGPointMake(530/ratio_w, 48/ratio_w);
        bpoint_d = CGPointMake(640/ratio_w, 48/ratio_w);
        //
        self.backgroundColor = [UIColor clearColor];
        UIView* blackkuang = [[UIView alloc] init];
        blackkuang.alpha = 0.5f;
        blackkuang.backgroundColor = [UIColor blackColor];
        [self addSubview:blackkuang];
        blackkuang.translatesAutoresizingMaskIntoConstraints = NO;
        [blackkuang lyt_alignBottomToParent];
        [blackkuang lyt_alignLeftToParent];
        [blackkuang lyt_alignRightToParent];
        [blackkuang lyt_alignTopToParentWithMargin:48/ratio_w];
        
        
        //
        UIImage* image = [UIImage imageNamed:@"talk_btn_record_normal.png"];
        CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
        btnRecord = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnRecord setImage:image forState:UIControlStateNormal];
        [btnRecord setImage:[UIImage imageNamed:@"talk_btn_record_down.png"] forState:UIControlStateHighlighted];
        btnRecord.frame = frame;
        btnRecord.center = bpoint_a;
        btnRecord.hidden = YES;
        [self addSubview:btnRecord];
        //
        btnPlay = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnPlay setImage:[UIImage imageNamed:@"talk_btn_play_normal.png"] forState:UIControlStateNormal];
        [btnPlay setImage:[UIImage imageNamed:@"talk_btn_play_down.png"] forState:UIControlStateHighlighted];
        btnPlay.frame = frame;
        btnPlay.center = bpoint_b;
        btnPlay.hidden = YES;
        [self addSubview:btnPlay];
        //
        image = [UIImage imageNamed:@"talk_btn_share_small_normal.png"];
        frame = CGRectMake(0, 0, image.size.width, image.size.height);
        btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnShare setImage:image forState:UIControlStateNormal];
        [btnShare setImage:[UIImage imageNamed:@"talk_btn_share_small_down.png"] forState:UIControlStateHighlighted];
        btnShare.frame = frame;
        btnShare.center = bpoint_c;
        btnShare.hidden = YES;
#if Talk_Upload_Share_Close == 0
        [self addSubview:btnShare];
#endif
        //
        btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnDelete setImage:[UIImage imageNamed:@"talk_btn_trash_small_normal.png"] forState:UIControlStateNormal];
        [btnDelete setImage:[UIImage imageNamed:@"talk_btn_trash_small_down.png"] forState:UIControlStateHighlighted];
        btnDelete.frame = frame;
        btnDelete.center = bpoint_d;
        btnDelete.hidden = YES;
        [self addSubview:btnDelete];
        
        
        //
        UIImageView* imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"talk_icon_questione.png"]];
        imgv.center = CGPointMake(64/ratio_w, 150/ratio_w);
        [self addSubview:imgv];
        //
        frame = CGRectMake(110/ratio_w, imgv.frame.origin.y, 300/ratio_w, 60/ratio_w);
        TalkDoubleLabel* shuoshuo = [[TalkDoubleLabel alloc] initWithFrame:frame MainText:@"说说你的想法:" subText:@"Questions for you:" sepRatio:0.56];
        shuoshuo.label_main.textColor = __TalkBaseBackColor;
        shuoshuo.label_main.font = [UIFont boldSystemFontOfSize:shuoshuo.label_main.frame.size.height*0.9];
        shuoshuo.label_sub.textColor = __TalkBaseBackColor;
        [self addSubview:shuoshuo];
        
        //
        if (qarr.count == 6) {
            //
            UIScrollView* scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(52/ratio_w, 200/ratio_w, frame_self.size.width, frame_self.size.height-200/ratio_w)];
            scrollview.backgroundColor = [UIColor clearColor];
            scrollview.showsHorizontalScrollIndicator = NO;
            scrollview.showsVerticalScrollIndicator = NO;
            scrollview.contentSize = CGSizeMake(frame_self.size.width, 96/ratio_w*3+32/ratio_w);
            [self addSubview:scrollview];
            //
            frame = CGRectMake(0, 30/ratio_w, 768/ratio_w, 52/ratio_w);
            TalkDoubleLabel* q1 = [[TalkDoubleLabel alloc] initWithFrame:frame MainText:[qarr objectAtIndex:0] subText:[qarr objectAtIndex:1] sepRatio:0.56 orderNumSign:@"1."];
            [scrollview addSubview:q1];
            //
            frame.origin.y = 96/ratio_w+30/ratio_w;
            TalkDoubleLabel* q2 = [[TalkDoubleLabel alloc] initWithFrame:frame MainText:[qarr objectAtIndex:2] subText:[qarr objectAtIndex:3] sepRatio:0.56 orderNumSign:@"2."];
            [scrollview addSubview:q2];
            //
            frame.origin.y = 96/ratio_w*2+30/ratio_w;
            TalkDoubleLabel* q3 = [[TalkDoubleLabel alloc] initWithFrame:frame MainText:[qarr objectAtIndex:4] subText:[qarr objectAtIndex:5] sepRatio:0.56 orderNumSign:@"3."];
            [scrollview addSubview:q3];
        }
    }
    return self;
}

- (void)show
{
    CGRect frame = self.frame;
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    frame.origin = CGPointMake(frame.origin.x, height-518/self.ratio);
    btnRecord.hidden = btnPlay.hidden = btnShare.hidden = btnDelete.hidden = YES;
    if (_needPlayBtn) {
        btnRecord.center = bpoint_a;
    }else{
        btnRecord.center = bpoint_b;
    }
    btnRecord.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    btnPlay.transform = btnShare.transform = btnDelete.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            self.frame = frame;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.4 animations:^{
            btnRecord.hidden = NO;
            btnRecord.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.4 relativeDuration:0.4 animations:^{
            if (_needPlayBtn) {
                btnPlay.hidden = btnShare.hidden = btnDelete.hidden = NO;
            }else{
                btnPlay.hidden = YES;
            }
            btnPlay.transform = btnShare.transform = btnDelete.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.1 animations:^{
            btnRecord.transform = CGAffineTransformMakeScale(1, 1);
            btnPlay.transform = btnShare.transform = btnDelete.transform = CGAffineTransformMakeScale(1, 1);
        }];
    } completion:^(BOOL finished) {
        btnRecord.transform = CGAffineTransformMakeScale(1, 1);
        btnPlay.transform = btnShare.transform = btnDelete.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
    self.isShown = YES;
}

- (void)unshow
{
    //TLDebugS(@"I am unshow");
    CGRect frame = self.frame;
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    frame.origin = CGPointMake(frame.origin.x, height);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.1 animations:^{
            btnRecord.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.1 animations:^{
            btnPlay.transform = btnShare.transform = btnDelete.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.6 animations:^{
            btnRecord.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            btnPlay.transform = btnShare.transform = btnDelete.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
        }];
        [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
            self.frame = frame;
        }];
    } completion:^(BOOL finished) {
        btnRecord.hidden = YES;
        btnPlay.hidden = btnShare.hidden = btnDelete.hidden = YES;
    }];
    
    self.isShown = NO;
}

@end
